<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>lvmx-express</title>
  <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" />
  <link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/5.11.2/css/all.min.css">
  <link rel="stylesheet" href="/stylesheets/style.css">
</head>

<body>
  <nav class="navbar navbar-dark bg-info navbar-expand-lg">
    <a class="navbar-brand" href="/">
      <img src="https://v4.bootcss.com/docs/4.3/assets/brand/bootstrap-solid.svg" width="30" height="30"
        class="d-inline-block align-top" alt="" />
      Bootstrap
    </a>

    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
          <a class="nav-link" href="/posts">文章列表</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/posts/create">文章新增</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/chat">聊天室</a>
        </li>
      </ul>

      <ul class="navbar-nav">
        <li class="nav-item">
          <a class="nav-link" href="/users/login">Login</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/users/register">Register</a>
        </li>
      </ul>
    </div>
  </nav>

  <div class="container my-5">
    <h3 class="mt-5 mb-4">添加新文章</h3>

    <form action="/posts" method="POST">
      <div class="form-group">
        <label for="title">标题</label>
        <input type="text" class="form-control" name="title" placeholder="请输入标题" autofocus />
      </div>

      <div class="form-group">
        <label for="content">正文</label>
        <textarea class="form-control" name="content" rows="8" cols="80" placeholder="请输入正文"></textarea>
      </div>

      <div class="form-group">
        <label for="content">图片</label>
        <div class="d-flex flex-wrap border rounded p-2">
          <!-- <div class="file-box">
            <img src="/images/avatar.png" alt="">
          </div> -->

          <!-- <div class="file-box">
            <img src="/images/avatar.png" alt="">
          </div>

          <div class="file-box">
            <img src="/images/avatar.png" alt="">
          </div>

          <div class="file-box">
            <img src="/images/avatar.png" alt="">
          </div> -->

          <div class="file-box">
            <input type="file" id="myFile">
            <div class="file-box__plus" onclick="myFile.click()">
              <i class="fa fa-plus fa-5x"></i>
            </div>
          </div>


          <input type="hidden" name="pictures">
        </div>
      </div>

      <div class="form-group d-flex justify-content-end">
        <button class="btn btn-info ml-2" type="reset">Reset</button>
        <button class="btn btn-primary ml-2" type="submit">Submit</button>
      </div>
    </form>
  </div>

  <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
  <script>
    $(() => {
      $('#myFile').change(() => {
        const formData = new FormData();
        formData.append('file', $('#myFile')[0].files[0]);

        $.ajax({
          url: '/files',
          method: 'POST',
          data: formData,
          processData: false, // 注意
          contentType: false, // 注意
          success: (res) => {
            if (res.code === 0) {
              let img = `
                <div class="file-box">
                  <img src="${res.data.url}" alt="">
                </div>
              `
              $('#myFile').parent().before(img)

              let picturesStr = $('input[name="pictures"]').val()
              let pictures = []
              if (picturesStr) {
                pictures = picturesStr.split(',')
              }
              pictures.push(res.data._id)
              $('input[name="pictures"]').val(pictures.join(','))
            }
          }
        })
      })
    })
  </script>
</body>

</html>